package com.yonyou.pmclouds.reason.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.offline.aspect.Offline;
import com.yonyou.pmclouds.offline.entity.OffLineDataKeyConst;
import com.yonyou.pmclouds.reason.entity.ProblemReasonRefVO;
import com.yonyou.pmclouds.reason.rmiitf.ProblemReasonQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/mobile/problemReason")
@RestController
@Offline(dataFrom = OffLineDataKeyConst.DATA_PROBLEM_REASON)
public class ProblemReasonMobController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProblemReasonQuery query;

    @GetMapping("/list")
    public ProblemReasonRefVO[] getTenantProblemReasons(@RequestParam("tenantId") String tenantId,
                                                        @RequestParam("category") Integer category) throws BusinessException {
        // TODO 这个需要切换成上下文传递
        ProblemReasonRefVO[] allVOs = query.queryReasonRefByTenant(tenantId, category.byteValue());
        //  前端要树，那就构造树吧
        if (ArrayUtils.isEmpty(allVOs)) {
            return allVOs;
        }
        // 构造树
        Map<String, List<ProblemReasonRefVO>> reasonChildMap = new HashMap<>();
        for (ProblemReasonRefVO vo : allVOs) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            }
            List<ProblemReasonRefVO> children = reasonChildMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                reasonChildMap.put(parent, children);
            }
            children.add(vo);
        }
        for (ProblemReasonRefVO vo : allVOs) {
            List<ProblemReasonRefVO> children = reasonChildMap.get(vo.getPkReason());
            if (children != null) {
                vo.setChildReason(children.toArray(new ProblemReasonRefVO[children.size()]));
            }
        }
        List<ProblemReasonRefVO> result = reasonChildMap.get(StringUtils.EMPTY);
        if (result == null) {
            return new ProblemReasonRefVO[0];
        } else {
            return result.toArray(new ProblemReasonRefVO[result.size()]);
        }
    }

    @Offline()
    public Object buildTree(Object object){

        ProblemReasonRefVO[] allVOs = (ProblemReasonRefVO[])object;
        // 构造树
        Map<String, List<ProblemReasonRefVO>> reasonChildMap = new HashMap<>();
        for (ProblemReasonRefVO vo : allVOs) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            }
            List<ProblemReasonRefVO> children = reasonChildMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                reasonChildMap.put(parent, children);
            }
            children.add(vo);
        }
        for (ProblemReasonRefVO vo : allVOs) {
            List<ProblemReasonRefVO> children = reasonChildMap.get(vo.getPkReason());
            if (children != null) {
                vo.setChildReason(children.toArray(new ProblemReasonRefVO[children.size()]));
            }
        }
        List<ProblemReasonRefVO> result = reasonChildMap.get(StringUtils.EMPTY);
        if (result == null) {
            return new ProblemReasonRefVO[0];
        } else {
            return result.toArray(new ProblemReasonRefVO[result.size()]);
        }

    }


}
